
#include <patronus_public.h>

#define MAX_SENDER_NUM	1		

static int num = 0;
static int user_id = 0;
static int from_user_id = 0;
static int to_user_id = 0;

volatile int run = 1;
static void *client_task(void *args)
{
	zctx_t *ctx = zctx_new();

	void *client = zsocket_new(ctx, ZMQ_DEALER);

	char identity [10];
	if (0 == user_id) {
    	sprintf (identity, "%04X-%04X", randof (0x10000), randof (0x10000));
	} else {
		sprintf (identity, "%X", from_user_id);
	}
	
	zsockopt_set_identity (client, identity);
	zsocket_connect(client, "tcp://*:8743");

	// generate the send message(stream)
	msg_head_t msg_head 		= {MSG_TYPE_CONNECT, 0, 0, 0, 0, time(NULL)};
	msg_connect_t msg_connect 	= {156702584335234ll, "lwoxx.fan@gmail.com"};

	zmsg_t *msg = zmsg_new();
	zframe_t *frame_head	= zframe_new((void *)&msg_head, sizeof(msg_head_t));
	zframe_t *frame_IMEI 	= zframe_new((void *)&msg_connect.IMEI, sizeof(long long));
	zframe_t *frame_email	= zframe_new(msg_connect.email, strlen(msg_connect.email));
	zframe_send(&frame_head, client, ZFRAME_MORE);
	zframe_send(&frame_IMEI, client, ZFRAME_MORE);
	zframe_send(&frame_email, client, 0);

	zmq_pollitem_t items[] = {{client, 0, ZMQ_POLLIN, 0}};
	int i = 0;
	while(1) {
		int centitick;
		for (centitick = 0; centitick < 100; centitick++) {
			zmq_poll (items, 1, 10*ZMQ_POLL_MSEC);
			if (items[0].revents & ZMQ_POLLIN) {
				zmsg_t *msg = zmsg_recv(client);
				zmsg_dump(msg);
				zmsg_destroy(&msg);
			}
		}
	
		//send to the text chat
		msg_head_t msg_head 			= {MSG_TYPE_CHAT_TEXT, 0, 0, from_user_id, to_user_id, time(NULL)};
		msg_chat_text_t msg_chat_text	= {"hi, i'm here"};
		
		zframe_t *frame_head = zframe_new((void *)&msg_head, sizeof(msg_head_t));
		zframe_t *frame_text = zframe_new((void *)msg_chat_text.chat_text,
				strlen(msg_chat_text.chat_text));
		zframe_send(&frame_head, client, ZFRAME_MORE);
		zframe_send(&frame_text, client, 0);
		i++;
		if (i == 20)
			break;
	}
	//zmsg_addmem(msg, (void *)&msg_connect, sizeof(msg_connect_t));
	//zmsg_send(&msg, client); 
//	for (i = 0; i < 20; i++) {
//		zstr_sendf(client, "Hello from %d:%d";

//		zclock_sleep(2 * 1000);
//	}

	zclock_sleep(3*1000);	
	zctx_destroy(&ctx);
	return NULL;
}

static void *client_send_task(void *args)
{
/*	int client_nbrs;
//	for (client_nbr = 0; client_nbr < 20; client_nbr++) {
		msg_chat_text_t msg_chat_text;
		msg_chat_text.msg_head.type = MSG_TYPE_CHAT_TEXT;
		msg_chat_text.msg_head.yb_device_id_from = 1;
		msg_chat_text.msg_head.yb_device_id_to = 1;
		
		zframe_t *frame = zframe_new((void *)&msg_chat_text, sizeof(msg_chat_text));
		zframe_send(&frame, client, 0);
	//	char *text = "hi i say u";
	//		memcpy(msg_chat_text, text, strlen(text));
//		msg_chat_text.
//	}
*/
}


int main(int args, char *argv[])
{
	if (args < 3) {
		fprintf(stdout, "usage: test [from_user_id] [to_user_id]\n");	
		return EXIT_FAILURE;
	}
	user_id = args < 3 ? 0 : atoi(argv[1]);
	from_user_id = atoi(argv[1]);
	to_user_id = atoi(argv[2]);

	zctx_t *ctx = zctx_new();
	int client_nbr;
	for (client_nbr = 0; client_nbr < MAX_SENDER_NUM; client_nbr++) {
		zthread_new(ctx, client_task, NULL);
	}

//	zthread_new(ctx, client_send_task, NULL);
	zclock_sleep(20 * 1000);
	zctx_destroy(&ctx);

	return EXIT_SUCCESS;
}
